package br.ucb.sigplan.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.ucb.sigplan.beans.Disciplina;
import br.ucb.sigplan.connection.FactoryCon;
import br.ucb.sigplan.dao.DisciplinaDAO;
import br.ucb.sigplan.exception.DAOException;

public class DisciplinaDAOImpl implements DisciplinaDAO{
	private final String SQL_INSERT = "insert into disciplina (nome,codigoCurriculo,ementa,contribuicao,objetivoGeral, objetivoEspecifico) values (?,?,?,?,?,?)";
	private final String SQL_FINDALL = "select * from disciplina";
	private final String SQL_SEARCHBYCOD = "select * from disciplina where codigoCurriculo = ? and nome = ?";
	private final String SQL_SEARCHBYID = "select * from disciplina where iddisciplina = ?";
	private final String SQL_DELETE = "delete from disciplina where iddisciplina = ?";
	private final String SQL_UPDATE = "update disciplina set nome = ?, codigoCurriculo = ?, ementa = ?, contribuicao = ?, objetivoGeral = ?,objetivoEspecifico = ? where iddisciplina = ?";
	
	public List<Disciplina> findAll() throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		List<Disciplina> disciplinas = new ArrayList<Disciplina>();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Disciplina disciplina;
		try {
			instrucao = con.prepareStatement(SQL_FINDALL);
			set = instrucao.executeQuery();
			while (set.next()) {
				disciplina = new Disciplina();
				putDisciplina(disciplina, set);
				disciplinas.add(disciplina);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao,set);
		}
		return disciplinas;
	}

	public void insert(Disciplina disciplina) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_INSERT);
			instrucao.setString(1, disciplina.getNome());
			instrucao.setInt(2, disciplina.getCodigoCurriculo());
			instrucao.setString(3, disciplina.getEmenta());
			instrucao.setString(4, disciplina.getContribuicao());
			instrucao.setString(5, disciplina.getObjetivoGeral());
			instrucao.setString(6, disciplina.getObjetivoEspecifico());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroInsercao");
		}finally{
			FactoryCon.closeConnection(con, instrucao);
		}
	}
	
	public void putDisciplina(Disciplina disciplina, ResultSet set) throws SQLException {
		disciplina.setCodigoCurriculo(set.getInt("codigoCurriculo"));
		disciplina.setContribuicao(set.getString("contribuicao"));
		disciplina.setEmenta(set.getString("ementa"));
		disciplina.setIdDisciplina(set.getInt("idDisciplina"));
		disciplina.setNome(set.getString("nome"));
		disciplina.setObjetivoEspecifico(set.getString("objetivoEspecifico"));
		disciplina.setObjetivoGeral(set.getString("objetivoGeral"));
	}

	public Disciplina findById(Integer iddisciplina) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		Disciplina disciplina = null;
		ResultSet set = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYID);
			instrucao.setInt(1, iddisciplina);
			set = instrucao.executeQuery();
			while (set.next()) {
				disciplina = new Disciplina();
				putDisciplina(disciplina, set);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return disciplina;
	}

	public Disciplina findByCod(Integer codigoCurriculo, String nome) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		Disciplina disciplina = null;
		ResultSet set = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYCOD);
			instrucao.setInt(1, codigoCurriculo);
			instrucao.setString(2, nome);
			set = instrucao.executeQuery();
			while (set.next()) {
				disciplina = new Disciplina();
				putDisciplina(disciplina, set);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return disciplina;
	}

	public boolean isDisciplinaCadastrada(Integer codigoCurriculo, String nome) {
		Disciplina disciplina;
		try {
			disciplina = findByCod(codigoCurriculo, nome);
		} catch (DAOException e) {
			e.printStackTrace();
			return false;
		}
		if (disciplina != null ) {
			return true;
		}
		return false;
	}

	@Override
	public void delete(Integer idDisciplina) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_DELETE);
			instrucao.setInt(1, idDisciplina);
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("disciplinaDelecao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}

	@Override
	public void update(Disciplina disciplina) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_UPDATE);
			instrucao.setString(1, disciplina.getNome());
			instrucao.setInt(2, disciplina.getCodigoCurriculo());
			instrucao.setString(3, disciplina.getEmenta());
			instrucao.setString(4, disciplina.getContribuicao());
			instrucao.setString(5, disciplina.getObjetivoGeral());
			instrucao.setString(6, disciplina.getObjetivoEspecifico());
			instrucao.setInt(7, disciplina.getIdDisciplina());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroAtualizacao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
		
	}
}
